//
//  MainViewController.swift
//  FM
//
//  Created by x on 2017/8/23.
//  Copyright © 2017年 HLB. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        //初始化
        addChildViewControllers()
    }
    
    private func addChildViewControllers() -> Void {
        initWithController(className: "HomeViewController", image: "tabBar_0", title: "首页")
        initWithController(className: "FindViewController", image: "tabBar_1", title: "发现")
        initWithController(className: "CommunityViewController", image: "tabBar_2", title: "社区")
        initWithController(className: "MeViewController", image: "tabBar_3", title: "我的")
    }
    
    /// 初始化控制器
    ///
    /// - Parameters:
    ///   - className: 控制器名字
    ///   - image: 图片
    ///   - title: 标题
    private func initWithController(className:String, image:String, title:String) -> Void {
        
        //取得空间名
        let DynameicNaming = Bundle.main.infoDictionary?["CFBundleExecutable"] as! String
        //把字符串准换成类
        let viewClass:AnyClass? = NSClassFromString(DynameicNaming+"."+className)
        //把类名转换成我们需要的控制器
        let viewController = viewClass as! UIViewController.Type
        //初始化控制器(只能用init()方法初始化)
        let vc = viewController.init()
        
        //设置属性
        vc.tabBarItem.image = UIImage.init(named: image)?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage.init(named: image+"_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        vc.tabBarItem.title = title
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.init(red: 234/255.0, green: 110/255.0, blue: 68/255.0, alpha: 1.0)], for: UIControlState.selected)
        
        let  navi = UINavigationController(rootViewController: vc)
        
        addChildViewController(navi)
    }
}
